#本py文件是显示coco数据下的骨骼点，骨骼点存储的数据格式为pickle

import pickle
import argparse
import numpy as np
import cv2
from mmaction.utils import frame_extract
import tempfile


def parse_arguments():
    parser = argparse.ArgumentParser(
        description="Load and visualize a pickle file.")
    parser.add_argument('--video_file', type=str, default="demo/myvideo/横踢0A001.mp4",
                        help="Path to the video file to be loaded.")
    parser.add_argument('--pickle_file', type=str, default="demo/pose3d/c.pkl",
                        help="Path to the pickle file to be loaded.")
    # parser.add_argument('--short-side',type=int,default=480,help='specify the short-side length of the image')
    # parser.add_argument('--pose-config',default='demo/demo_configs/td-hm_hrnet-w32_8xb64-210e_coco-256x192_infer.py',help='human pose estimation config file path (from mmpose)')
    return parser.parse_args()


def main():
    # COCO数据集上17个关键点的连接方式
    links = [
        (15, 13), (13, 11), (16, 14), (14, 12), (11,
                                                 12), (5, 11), (6, 12), (5, 6), (5, 7),
        (6, 8), (7, 9), (8, 10), (1, 2), (0, 1), (0, 2),
        (1, 3), (2, 4), (3, 5), (4, 6)
    ]
    args = parse_arguments()

    final_data = []
    # 读取pickle数据
    # with open(args.pickle_file, 'rb') as file:
    #     data = pickle.load(file)

    # final_data.append(data)

    # with open("demo/pose3d/a.pkl", 'rb') as file:
    #     data = pickle.load(file)

    # final_data.append(data)

    # print(final_data)

    # print(data)
    # keypoints = data['keypoint']

    # # 读取视频
    # cap = cv2.VideoCapture(args.video_file)

    # frame_count = 0
    # # 获取视频的FPS(frames per second)
    # fps = int(cap.get(cv2.CAP_PROP_FPS))
    # while cap.isOpened():
    #     ret, frame = cap.read()

    #     if not ret:
    #         break

    #     # 获取该帧的所有关键点
    #     frame_keypoints = keypoints[:, frame_count, :, :]

    #     # 对于每个人
    #     for person_keypoints in frame_keypoints:
    #         for i, j in links:
    #             pt1 = tuple(person_keypoints[i, :].astype(int))
    #             pt2 = tuple(person_keypoints[j, :].astype(int))
    #             cv2.line(frame, pt1, pt2, (0, 255, 0), 2)
    #             cv2.circle(frame, pt1, 3, (0, 0, 255), -1)
    #             cv2.circle(frame, pt2, 3, (0, 0, 255), -1)

    #     cv2.imshow('Skeleton Visualization', frame)

    #     frame_count += 1
    #     if cv2.waitKey(1000 // fps) & 0xFF == ord('q'):
    #         break

    # cap.release()
    # cv2.destroyAllWindows()


if __name__ == "__main__":
    main()


# def visualize(args, meta_data, frames, data_samples, action_label):
#     pose_config = mmengine.Config.fromfile(args.pose_config)
#     visualizer = VISUALIZERS.build(pose_config.visualizer)
#     visualizer.set_dataset_meta(meta_data)

#     vis_frames = []
#     print('Drawing skeleton for each frame')
#     for d, f in track_iter_progress(list(zip(data_samples, frames))):
#         f = mmcv.imconvert(f, 'bgr', 'rgb')
#         visualizer.add_datasample(
#             'result',
#             f,
#             data_sample=d,
#             draw_gt=False,
#             draw_heatmap=False,
#             draw_bbox=True,
#             show=False,
#             wait_time=0,
#             out_file=None,
#             kpt_thr=0.3)
#         vis_frame = visualizer.get_image()
#         cv2.putText(vis_frame, action_label, (10, 30), FONTFACE, FONTSCALE,
#                     FONTCOLOR, THICKNESS, LINETYPE)
#         vis_frames.append(vis_frame)

#     vid = mpy.ImageSequenceClip(vis_frames, fps=24)
#     vid.write_videofile(args.out_filename, remove_temp=True)

# def main():
#     args = parse_arguments()
#     tmp_dir = tempfile.TemporaryDirectory()
#     frame_paths, frames = frame_extract(args.video_file, args.short_side,tmp_dir.name)
#     # print(type(frame_paths))
#     # print(type(frames))
#     # print(len(frame_paths))
#     # print(len(frames))
#     # print(frame_paths[0])
#     # print(frames[0].shape)
#     with open('demo/mycode/data.pkl', 'rb') as f:
#         meta_data=pickle.load(f)

#     with open(args.pickle_file, 'rb') as file:
#         data = pickle.load(file)
#     print(type(data))
#     print(len(data))
#     # print(meta_dataset)
#     # print(type(meta_dataset))
